﻿using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XJLSC.QM;

namespace XJLSC.PM
{
    public partial class NLGL : Form
    {
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        SCKC f;
        DDKC g;
        public NLGL()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
               string strSql = "";
               strSql = "SELECT MO.DOC_NO 转入工单,MO.MO_ID,MO_D.MO_D_ID,ITEM_CODE 材料品号,ITEM_NAME 材料品名,MO_D.ITEM_SPECIFICATION 材料规格,CAST(MO_D.REQUIRED_QTY AS REAL) 需领数量,MO_D.REMARK 备注,MO.SOURCE_ID_RTK SOURCEIDRTK,MO.SOURCE_ID_ROid SOURCEIDROid,MO.Owner_Org_ROid FROM MO_D LEFT JOIN MO ON MO.MO_ID=MO_D.MO_ID LEFT JOIN ITEM ON ITEM.ITEM_BUSINESS_ID=MO_D.ITEM_ID WHERE MO.ApproveStatus='Y' AND MO.[STATUS]<>'Y' AND MO.[STATUS]<>'y' AND ITEM_CODE LIKE '%" + textBox1.Text + "%' AND  ITEM_NAME LIKE '%" + textBox2.Text + "%' AND MO_D.ITEM_SPECIFICATION LIKE '%" + textBox3.Text + "%' AND MO.DOC_NO LIKE '%" + textBox4.Text + "%'";
                //textBox4.Text = strSql;
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl1.DataSource = dt;

            }
            catch
            {

            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
       
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                string strSql = "";
                strSql = "SELECT MO.DOC_NO 转出工单,MO.MO_ID,MO_D.MO_D_ID,ITEM_CODE 品号,ITEM_NAME 品名,MO_D.ITEM_SPECIFICATION 规格,'' 批号,(SELECT TOP 1 WAREHOUSE_ID FROM ISSUE_RECEIPT_D WHERE ITEM_ID=MO_D.ITEM_ID AND ISSUE_RECEIPT_D.MO_ID=MO.MO_ID) 仓库,CAST(MO_D.ISSUED_QTY AS REAL) 已领数量,ROUND(CAST(ABS(MO_D.REQUIRED_QTY/MO.PLAN_QTY*ISNULL(MO.COMPLETED_QTY,0)) AS REAL),0) 需领用量,CAST(MO_D.ISSUED_QTY AS REAL)-ROUND(CAST(ABS(MO_D.REQUIRED_QTY/MO.PLAN_QTY*ISNULL(MO.COMPLETED_QTY,0)) AS REAL),0) 可挪数量,MO_D.REMARK 备注 FROM MO_D LEFT JOIN MO ON MO.MO_ID=MO_D.MO_ID LEFT JOIN ITEM ON ITEM.ITEM_BUSINESS_ID=MO_D.ITEM_ID WHERE MO.ApproveStatus='Y' AND MO.[STATUS]<>'Y' AND MO.[STATUS]<>'y' AND ITEM_CODE='" + gridView1.GetFocusedRowCellValue("材料品号").ToString() + "' AND MO.SOURCE_ID_RTK='" + gridView1.GetFocusedRowCellValue("SOURCEIDRTK").ToString() + "' AND MO.SOURCE_ID_ROid='" + gridView1.GetFocusedRowCellValue("SOURCEIDROid").ToString() + "' AND CAST(MO_D.ISSUED_QTY AS REAL)-ROUND(CAST(ABS(MO_D.REQUIRED_QTY/MO.PLAN_QTY*ISNULL(MO.COMPLETED_QTY,0)) AS REAL),0)>0";
                //textBox4.Text = strSql;
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl2.DataSource = dt;
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
        
            String gdhr = this.gridView1.GetFocusedRowCellValue("转入工单").ToString();
            String modidr = this.gridView1.GetFocusedRowCellValue("MO_ID").ToString();
            String moddidr = this.gridView1.GetFocusedRowCellValue("MO_D_ID").ToString();
            String clph = this.gridView1.GetFocusedRowCellValue("材料品号").ToString();
            String clpm = this.gridView1.GetFocusedRowCellValue("材料品名").ToString();
            String clgg = this.gridView1.GetFocusedRowCellValue("材料规格").ToString();
            String  sourceidrtk= this.gridView1.GetFocusedRowCellValue("SOURCEIDRTK").ToString();
            String sourceidroid = this.gridView1.GetFocusedRowCellValue("SOURCEIDROid").ToString();
            String ownerorgroid = this.gridView1.GetFocusedRowCellValue("Owner_Org_ROid").ToString();
            int[] rows = this.gridView2.GetSelectedRows();
            foreach (int row in rows)
            {
                String gdhc = this.gridView2.GetRowCellValue(row, "转出工单").ToString();
                String moidc = this.gridView2.GetRowCellValue(row, "MO_ID").ToString();
                String moddidc = this.gridView2.GetRowCellValue(row, "MO_D_ID").ToString();

                String cpph = this.gridView2.GetRowCellValue(row, "批号").ToString();
                String clsl = this.gridView2.GetRowCellValue(row, "可挪数量").ToString();
                String clbz = this.gridView2.GetRowCellValue(row, "备注").ToString();
                String clck = this.gridView2.GetRowCellValue(row, "仓库").ToString();

                gridView3.AddNewRow();
                gridView3.SetFocusedRowCellValue("转入工单",gdhr);
                gridView3.SetFocusedRowCellValue("RMO_ID", modidr);
                gridView3.SetFocusedRowCellValue("RMO_D_ID", moddidr);
                gridView3.SetFocusedRowCellValue("转出工单", gdhc);
                gridView3.SetFocusedRowCellValue("CMO_ID", moidc);
                gridView3.SetFocusedRowCellValue("CMO_D_ID", moddidc);
                gridView3.SetFocusedRowCellValue("材料品号", clph);
                gridView3.SetFocusedRowCellValue("材料品名", clpm);
                gridView3.SetFocusedRowCellValue("材料规格", clgg);
                gridView3.SetFocusedRowCellValue("批号", cpph);
                gridView3.SetFocusedRowCellValue("数量", clsl);
                gridView3.SetFocusedRowCellValue("备注", clbz);
                gridView3.SetFocusedRowCellValue("SOURCEIDRTK", sourceidrtk);
                gridView3.SetFocusedRowCellValue("SOURCEIDROid", sourceidroid);
                gridView3.SetFocusedRowCellValue("Owner_Org_ROid", ownerorgroid);
                gridView3.SetFocusedRowCellValue("仓库", clck);
            }
            gridView3.OptionsBehavior.Editable = true;
            gridView3.PopulateColumns();
        }

        private void gridView3_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
         
            //String gdhr = this.gridView1.GetFocusedRowCellValue("转入工单").ToString();
            //String modidr = this.gridView1.GetFocusedRowCellValue("MO_ID").ToString();
            //String moddidr = this.gridView1.GetFocusedRowCellValue("MO_D_ID").ToString();
            //gridView3.SetFocusedRowCellValue("R工单号", gdhr);
            //gridView3.SetFocusedRowCellValue("RMO_ID", modidr);
            //gridView3.SetFocusedRowCellValue("RMO_D_ID", moddidr);
        }

        private void NLGL_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable("明细");
            dt.Columns.Add("转入工单", typeof(string));
            dt.Columns.Add("RMO_ID", typeof(string));
            dt.Columns.Add("RMO_D_ID", typeof(string));
            dt.Columns.Add("转出工单", typeof(string));
            dt.Columns.Add("CMO_ID", typeof(string));
            dt.Columns.Add("CMO_D_ID", typeof(string));
            dt.Columns.Add("材料品号", typeof(string));
            dt.Columns.Add("材料品名", typeof(string));
            dt.Columns.Add("材料规格", typeof(string));
            dt.Columns.Add("批号", typeof(string));
            dt.Columns.Add("数量", typeof(string));
            dt.Columns.Add("备注", typeof(string));
            dt.Columns.Add("SOURCEIDRTK", typeof(string));
            dt.Columns.Add("SOURCEIDROid", typeof(string));
            dt.Columns.Add("Owner_Org_ROid", typeof(string));
            dt.Columns.Add("仓库", typeof(string));
            gridControl3.DataSource = dt;

            textBox6.Text = "5710";
            textBox5.Text = "5640";
            textBox7.Text = "000";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String strSql;
            strSql = "DELETE FROM MO_TEMP_LTL1 WHERE DLH='" + PropertyClass.OperatorCode + "'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                }
                else
                {
                }
            }
            catch
            {
            }
            String CK="";
            strSql = "SELECT WAREHOUSE_ID FROM WAREHOUSE WHERE WAREHOUSE_CODE='" + textBox7.Text + "'";
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {
                    CK = sdr1["WAREHOUSE_ID"].ToString();
                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message, "软件提示");
               throw ex;
            }
            finally
            {
              sdr1.Close();
            }
            int[] rows = this.gridView3.GetSelectedRows();
            foreach (int row in rows)
            {
                String mo_id = this.gridView3.GetRowCellValue(row, "RMO_ID").ToString();
                String mo_d_id = this.gridView3.GetRowCellValue(row, "RMO_D_ID").ToString();
                String cmo_id = this.gridView3.GetRowCellValue(row, "CMO_ID").ToString();
                String cmo_d_id = this.gridView3.GetRowCellValue(row, "CMO_D_ID").ToString();
                String source_rtk = this.gridView3.GetRowCellValue(row, "SOURCEIDRTK").ToString();
                String source_roid = this.gridView3.GetRowCellValue(row, "SOURCEIDROid").ToString();
                String qty = this.gridView3.GetRowCellValue(row, "数量").ToString();
                String owner = this.gridView3.GetRowCellValue(row, "Owner_Org_ROid").ToString();
                strSql = "INSERT MO_TEMP_LTL1 SELECT '" + PropertyClass.OperatorCode + "','" + cmo_id + "','" + cmo_d_id + "','" + source_rtk + "','" + source_roid + "'," + qty + ",'','" + owner + "','" + CK + "',''";
                //textBox3.Text = strSql;
                //return;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                    }
                }
                catch
                {

                }
            }
            strSql = "EXEC [SP_MO_TEMP_LTL1] '" + PropertyClass.OperatorCode + "','" + textBox5.Text + "','" + textBox7.Text + "','0','" + DateTime.Today + "'";
            //textBox3.Text = strSql;
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                   
                }
                else
                {

                }
            }
            catch
            {

            }

            strSql = "DELETE FROM MO_TEMP_LTL1 WHERE DLH='" + PropertyClass.OperatorCode + "'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                }
                else
                {
                }
            }
            catch
            {
            }
            int[] Trows = this.gridView3.GetSelectedRows();
            foreach (int row in Trows)
            {
                String mo_id = this.gridView3.GetRowCellValue(row, "RMO_ID").ToString();
                String mo_d_id = this.gridView3.GetRowCellValue(row, "RMO_D_ID").ToString();
                String source_rtk = this.gridView3.GetRowCellValue(row, "SOURCEIDRTK").ToString();
                String source_roid = this.gridView3.GetRowCellValue(row, "SOURCEIDROid").ToString();
                String qty = this.gridView3.GetRowCellValue(row, "数量").ToString();
                String owner = this.gridView3.GetRowCellValue(row, "Owner_Org_ROid").ToString();
                strSql = "INSERT MO_TEMP_LTL1 SELECT '" + PropertyClass.OperatorCode + "','" + mo_id + "','" + mo_d_id + "','" + source_rtk + "','" + source_roid + "'," + qty + ",'','" + owner + "','" + CK + "',''";
                //textBox6.Text = strSql;
                //return;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                    }
                }
                catch
                {

                }
            }

            strSql = "EXEC [SP_MO_TEMP_LTL1] '" + PropertyClass.OperatorCode + "','" + textBox6.Text + "','" + textBox7.Text + "','0','" + DateTime.Today + "'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                  
                }
                else
                {

                }
            }
            catch
            {

            }
            MessageBox.Show("挪料单生成成功！", "软件提示");
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView3.DeleteSelectedRows();
        }
    }
}
